//
//  User.m
//  gee
//
//  Created by gakaki on 11-5-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "User.h"

@implementation User

@synthesize user_name;
@synthesize user_pwd;
@synthesize uuid;

+(NSString *)device_uuid;{
   return [[UIDevice currentDevice] uniqueIdentifier];
}

+(BOOL)is_user_first_install{

    //[[NSUserDefaults standardUserDefaults] setValue:NULL forKey:@"first install"];
    
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"first install"] == NO) {
        CLog(@"this is first install");
        [[NSUserDefaults standardUserDefaults] setValue:[[UIDevice currentDevice] uniqueIdentifier] forKey:@"first install"];
        CLog(@"%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"first install"]);
        return YES;
    }
    return NO;
}

-(NSDictionary *)user_register
{
    
  return [[[NSDictionary alloc]initWithDictionary:nil] autorelease];
}

-(NSDictionary *)user_login{
    
    NSURL *url = [NSURL URLWithString:@"http://api.loc/cookie.php"];
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setPostValue:self.user_name     forKey:@"username"];
    [request setPostValue:self.user_pwd      forKey:@"password"];
    
    //Create a cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
    
    [properties setValue:[[[UIDevice currentDevice] uniqueIdentifier] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
        forKey:NSHTTPCookieValue];
    
    [properties setValue:@"PHPSESSID" forKey:NSHTTPCookieName];
    [properties setValue:@".api.loc" forKey:NSHTTPCookieDomain];
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] 
                forKey:NSHTTPCookieExpires];
    
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60] forKey:NSHTTPCookieExpires];
    
    [properties setValue:@"/" forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
    
    
    [request setUseCookiePersistence:NO];
    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];
    
    [request startAsynchronous];
    
    
    return [[[NSDictionary alloc]initWithDictionary:nil] autorelease];
}


#pragma mark -
#pragma mark HTTP Response Handlers

- (void)requestFinished:(ASIHTTPRequest *)request {
    // Use when fetching text data
    CLog(@"HTTP Fetch: %@", [request responseString]);
    
    
    CLog(@"Object jsonkit is : %@", [[request responseString] objectFromJSONString]);
    
    
    //保存用户名密码
    [[NSUserDefaults standardUserDefaults] setValue:@"gakaki" forKey:@"username"];
    [[NSUserDefaults standardUserDefaults] setValue:@"z5896321" forKey:@"first password"];
    
    id object =  [[request responseString] objectFromJSONString];
    //NSLog(@"%@", NSStringFromClass([object class]));
    
    NSDictionary *deserializedData =  [[request responseString] objectFromJSONString];
    NSLog(@"%@", deserializedData);
    
    
    
    NSLog(@"%@", [deserializedData valueForKey:@"status"]);
    NSLog(@"%@", [[deserializedData valueForKey:@"data"] valueForKey:@"password"]);
    
    [[NSUserDefaults standardUserDefaults] setValue:@"z5896321" forKey:@"site_session_id"];
    
    
    
    
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"Error: %@", [error description]);
    
}




@end
